/*
 * @Author: your name
 * @Date: 2021-12-20 14:52:58
 * @LastEditTime: 2022-01-03 10:29:45
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \vue_test\src\store\index.js
 */
//该文件用于创建Vuex中最为核心的store
import Vue from "vue";
//引入Vuex
import Vuex from "vuex";
//应用Vuex
Vue.use(Vuex);
//准备Actions——用于响应组件中的动作,业务逻辑全都写于此
const actions = {
  jia(context, value) {
    console.log("actions中的jia被调用了！");
    context.commit("JIA", value); //写成大写是区分一下，表示mutation中的加，因为mutation可以直接操作sum
  },
  jian(context, value) {
    console.log("actions中的jian被调用了！");
    context.commit("JIAN", value);
  },
  jiaOdd(context, value) {
    console.log("actions中的jiaOdd被调用了！", context);
    //console.log("处理了一些事情--jiaOdd");
    //context.dispatch("demo1", value);
    if (context.state.sum % 2) {
      context.commit("JIAODD", value);
    }
  },
  jiaWait(context, value) {
    console.log("actions中的jiaWait被调用了！");
    setTimeout(() => {
      context.commit("JIAWAIT", value);
    }, 300);
  },
};

//准备Mutations——用于操作数据（state），不写业务逻辑和发ajax请求
const mutations = {
  JIA(state, value) {
    console.log("mutations中的JIA被调用了！");
    state.sum += value;
  },
  JIAN(state, value) {
    console.log("mutations中的JIAN被调用了！");
    state.sum -= value;
  },
  JIAODD(state, value) {
    console.log("mutations中的JIAODD被调用了！");
    state.sum += value;
  },
  JIAWAIT(state, value) {
    console.log("mutations中的JIAWAIT被调用了！");
    state.sum += value;
  },
  Add_PERSON(state, value) {
    console.log("mutations中的Add_PERSON被调用了！");
    state.personList.unshift(value);
  },
};

//准备State——用于存储数据
const state = {
  sum: 0,
  school: "尚硅谷",
  subject: "前端",
  personList: [{ id: "001", name: "张三" }],
};
//准备getters——用于将state中的数据进行加工
const getters = {
  //可以收到参数
  bigSum(state) {
    return state.sum * 10;
  },
};
export default new Vuex.Store({
  actions,
  mutations,
  state,
  getters,
});
